package com.running.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

//
public class FileOperator {
	//
	public static boolean write(String dir,String name,String content) {
		boolean flag = false;
		try{
			new File(dir).mkdir();
			OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(dir + "/" + name),"UTF-8");
			out.write(content);
			out.close();
			flag = true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return flag;
	}
	//
	public static String read(String file) {
		String content = "";
		try{			
			FileReader reader = new FileReader(file);
			File f = new File(file);
			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f),"UTF-8"));
			String row = "";
			while ((row = br.readLine()) != null) {
				content += row+"\r\n";
			}
			br.close();
			reader.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		return content;
	}
	
	//
	public static boolean exists(String file) {
		boolean flag = false;
		try {
			File f = new File(file);
			flag = f.exists();
		} catch (Exception ex) {
			flag = false;
		}
		return flag;
	}
	
	//
	public static long size(String file) {
		long size = 0;
		try {
			File f = new File(file);
			if (f.exists()) {
				size = f.length();
			}
		} catch (Exception ex) {
		}
		return size;
	}
	
	//
	public static String getExtName(String file) {
		String extName = "";
		String[] tmpFileName = file.split("\\.");
		if (tmpFileName.length > 1) {
			extName = tmpFileName[tmpFileName.length-1];
		}
		return extName;
	}
}
